package 优先算法.前缀和.连续数组;

import java.util.Arrays;
import java.util.HashMap;

/**
 *
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {0,0,0,0,1,1,1,0};
        int rel = so.findMaxLength(arr);
        System.out.println(rel);
    }
}
class Solution {
    public int findMaxLength(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0,-1);
        //初始化数组 0->-1
        for(int i = 0; i < nums.length; i++){
           if(nums[i] == 0){
               nums[i] = -1;
           }
        }
        int sum = 0,rel = 0;
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
            if(map.containsKey(sum)){
                rel = Math.max(rel,i-map.get(sum));
            } else{
                map.put(sum,i);
            }
        }
        return rel;
    }
}